package classfile

type LocalVariableTypeTableAttr struct {
	AttrStruct
	localVariableTypeTableLength uint16
	localVariableTypeTables      []LocalVariableTypeTable
}

type LocalVariableTypeTable struct {
	// 变量开始生效的行
	startPc uint16
	// 变量影响范围
	length uint16
	// 变量名称
	localVariableTypeNameIndex uint16
	localVariableTypeName      Constant
	signatureIndex             uint16
	signature                  Constant
	// 序号
	localVariableTypeIndex uint16
}

func (self *LocalVariableTypeTableAttr) readAttr(cp ConstantPool) AttributeInfo {
	self.localVariableTypeTableLength = self.attributeData.ReadUInt16()
	if self.localVariableTypeTableLength > 0 {
		self.localVariableTypeTables = make([]LocalVariableTypeTable, self.localVariableTypeTableLength)
		for i := 0; i < int(self.localVariableTypeTableLength); i++ {
			self.localVariableTypeTables[i] = NewLocalVariableTypeTable(&self.attributeData, cp)
		}
	}
	return self
}

func NewLocalVariableTypeTable(data *AttributeData, cp ConstantPool) LocalVariableTypeTable {
	startPc := data.ReadUInt16()
	Length := data.ReadUInt16()
	LocalVariableNameIndex := data.ReadUInt16()
	LocalVariableDescriptorIndex := data.ReadUInt16()
	LocalVariableName_ := cp.GetConstant(LocalVariableNameIndex)
	LocalVariableDescriptor_ := cp.GetConstant(LocalVariableDescriptorIndex)
	LocalVariableIndex := data.ReadUInt16()
	return LocalVariableTypeTable{startPc, Length, LocalVariableNameIndex, LocalVariableName_,
		LocalVariableDescriptorIndex, LocalVariableDescriptor_, LocalVariableIndex}
}
